
from ..models import ent, ent_order
from ..models import ent_jobs
from ..models import ent_baseInfo
from .. import *


# 企业用户注册
@csrf_exempt
def entSubmitUser(request):
    message = json.loads(request.body)
    username = message['username']
    email = message['email']
    tel = message['tel']
    password = message['password']

    entUserNameInner = ent.objects.filter(username=username).first()
    entEmailInner = ent.objects.filter(email=email).first()
    entTelInner = ent.objects.filter(tel=tel).first()

    if entUserNameInner:
        return ErrResponse('用户名已存在，请更换')
    if entEmailInner:
        return ErrResponse('邮箱已被注册，请更换')
    if entTelInner:
        return ErrResponse('手机号已被注册，请更换')
    saveNewEntId = getId()
    ent(entId=saveNewEntId, username=username,
        password=password, email=email, tel=tel).save()
    return Response(saveNewEntId)


# 企业登录
@csrf_exempt
def entLogin(request):
    message = json.loads(request.body)
    username = message['username']
    password = message['password']
    try:
        m = ent.objects.get(username=username)
    except ent.DoesNotExist:
        return AccountNotFoundResponse()
    if m.password == password:
        request.session['entId'] = m.entId
        return Response(msg=m.entId)
    return LoginErrorResponse()


# 企业保存基本信息
@csrf_exempt
def setEntBaseInfo(request):
    message = json.loads(request.body)

    entName = message['entName']
    entCertId = message['entCertId']
    entId = message['entId']
    if ent_baseInfo.objects.filter(entName=entName).first():

        return ErrResponse('企业名已存在，请更换')
    if ent_baseInfo.objects.filter(entCertId=entCertId).first():

        return ErrResponse('营业执照已被占用，请确认')

    ent_baseInfo(entId=entId,
                 entName=entName,
                 entContectName=message['entContectName'],
                 entAddress=message['entAddress'],
                 entCertId=entCertId,
                 entClass=message['entClass'],
                 entScale=message['entScale'],
                 entSummary=message['entSummary']).save()
    request.session['entId'] = entId
    return Response()


# 查询基本信息
@csrf_exempt
def getEntBaseInfo(request):
    message = json.loads(request.body)
    data = {
    }
    entId = message['entId']
    data['entBaseInfo'] = model_to_dict(
        ent_baseInfo.objects.get(entId=entId))
    data['entAccountInfo'] = model_to_dict(
        ent.objects.get(entId=entId))
    return Response(msg=data)


@csrf_exempt
def getEntJob(request):
    message = json.loads(request.body)
    data = []
    entId = message['entId']
    jobId = ""
    if 'jobId' in message:
        jobId = message['jobId']
    if isErrorId(entId):
        return ErrResponse()
    if isErrorId(jobId):
        data = serializers.serialize(
            'json', ent_jobs.objects.order_by("updateTime").filter(entId=entId))
        return Response(msg=data)
    res = model_to_dict(
        ent_jobs.objects.get(jobId=jobId))
    return Response(msg=res)


# 企业提交基本信息
@csrf_exempt
def updateEntBaseInfo(request):
    message = json.loads(request.body)

    entId = message['entId']
    m = message['msg']
    entUsername = ent.objects.filter(username=m['username']).first()
    entEmail = ent.objects.filter(email=m['email']).first()
    entTel = ent.objects.filter(tel=m['tel']).first()
    entName = ent_baseInfo.objects.filter(entName=m['entName']).first()
    entCertId = ent_baseInfo.objects.filter(entCertId=m['entCertId']).first()
    if entUsername:
        if entUsername.entId != entId:
            return ErrResponse('用户名已存在，请更换')
    if entEmail:
        if entEmail.entId != entId:
            return ErrResponse('邮箱已被注册，请更换')
    if entTel:
        if entTel.entId != entId:
            return ErrResponse('手机已被注册，请更换')
    if entName:
        if entName.entId != entId:
            return ErrResponse('企业名已存在，请更换')
    if entCertId:
        if entCertId.entId != entId:
            return ErrResponse('营业执照已被占用，请确认')
    ent(entId=entId, tel=m['tel'], email=m['email'],
        username=m['username'], password=m['password']).save()
    ent_baseInfo(entId=entId, entName=m['entName'], entAddress=m['entAddress'], entCertId=m['entCertId'], entClass=m['entClass'],
                 entScale=m['entScale'], entSummary=m['entSummary'], entTag=m['entTag'],
                 ).save()
    return Response('保存成功')


# 企业提交职位信息
@csrf_exempt
def saveEntJobInfo(request):
    message = json.loads(request.body)
    entId = message['entId']
    entInfo = ent_baseInfo.objects.get(entId=entId)
    entName = entInfo.entName
    entTag = entInfo.entTag
    m = message['msg']
    jobId = message['jobId']
    if isErrorId(jobId):
        jobId = getId()
    ent_jobs(jobId=jobId, entName=entName, entId=entId,
             entTag=entTag, jobName=m['jobName'],
             jobType=m['jobType'], jobPay=m['jobPay'],
             jobAddress=m['jobAddress'], workExp=m['workExp'], degree=m['degree'], jobReq=m['jobReq'], email=m['email'],
             updateTime=datetime.date.today()).save()
    ent_base_inner = ent.objects.get(entId=entId)
    jobCount = ent_base_inner.entJobCount - 1
    ent_base_inner.entJobCount = jobCount
    ent_base_inner.save()
    return Response()


# 删除职位信息
@csrf_exempt
def delEntJobInfo(request):
    message = json.loads(request.body)
    ent_jobs.objects.get(jobId=message['msg']).delete()
    return Response()


@csrf_exempt
def getEntOrder(request):
    message = json.loads(request.body)
    data = []
    entId = message['entId']
    if isErrorId(entId):
        return ErrResponse()
    data = serializers.serialize(
        "json", ent_order.objects.filter(entId=entId).all())
    return Response(msg=data)
